package com.fast.admin.controller;
import com.fast.admin.common.utils.Const;
import com.fast.common.base.BaseController;
import com.fast.common.util.VerifyCodeUtil;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by 尘埃 on 2017/9/4 16:41.
 */
@RestController
@RequestMapping(value = "/sys/utils")
public class UtilsController extends BaseController {

    @Override
    protected String getPrefix() {
        return null;
    }

    /**
     * 生成登录验证码
     *
     * @return
     */
    @RequestMapping(value = "/verifyCode")
    public void show() {
        //设置页面不缓存
        this.response().setHeader("Pragma", "no-cache");
        this.response().setHeader("Cache-Control", "no-cache");
        this.response().setDateHeader("Expires", 0);
        //为了手机客户端方便使用数字验证码
        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        session.setAttribute(Const.VERIFY_CODE, verifyCode);
        try {
            //设置输出的内容的类型为JPEG图像
            this.response().setContentType("image/jpeg");
            BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null, null);
            //写给浏览器
            ImageIO.write(bufferedImage, "JPEG", this.response().getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 下载
     */
    @RequestMapping(value = "/download")
    public void download() {
        this.response().reset();//设置为没有缓存
        this.response().setContentType("application/x-download;charset=utf-8");
        this.response().setHeader("Content-Type", "application/force-download");
        OutputStream os = null;
        InputStream serverFile = null;
        try {
           String path = this.request().getParameter("path");
            //org.springframework.core.io.Resource fileRource = new ClassPathResource(path);
            File file = new File(path);
            this.response().setContentLength((int) file.length());
            this.response().setHeader("Content-disposition", "attachment;filename=" + new String(file.getName().getBytes(), "iso-8859-1"));
            os = this.response().getOutputStream();
            serverFile = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int count = 0;
            while ((count = serverFile.read(buffer)) > 0) {
                os.write(buffer, 0, count);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        } finally {
            IOUtils.closeQuietly(serverFile);
            IOUtils.closeQuietly(os);
        }
    }
}
